/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.dao.PostCategoryDAO;
import model.dto.PostCategory;
import model.util.SharingUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author PhucAn
 */
public class PostsCategoryAction extends BaseAction {

    PostCategoryDAO daoPCate = new PostCategoryDAO();

    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        PostCategory cate = new PostCategory();
        cate.setName(request.getParameter("name") == null ? "" : request.getParameter("name"));
        String strId = request.getParameter("parent-id") == null ? "" : request.getParameter("parent-id");
        if (strId.trim().equals("")) {
            strId = "0";
        }
        cate.setId(Integer.parseInt(strId));

        if (daoPCate.create(cate) > 0) {
            SharingUtil.showMessage(request, new ActionMessage("create.success"));
        } else {
            SharingUtil.showMessage(request, new ActionMessage("create.failure"));
        }
        request.setAttribute(CONTENT_HOLDER, "pcate-manage");
        return mapping.findForward("list");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("selected-item-id")==null?"0":request.getParameter("selected-item-id"));
        if(daoPCate.delete(id)>0) SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_SUCCESS));
        else SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_FAILURE));
        request.setAttribute(CONTENT_HOLDER, "pcate-manage");
        return mapping.findForward("list");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(SUCCESS);
    }

    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(SUCCESS);
    }

    public ActionForward manage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ArrayList<PostCategory> list = daoPCate.getAllCategories();
        request.setAttribute("list", list);
        request.setAttribute(CONTENT_HOLDER, "pcate-manage");        
        return mapping.findForward("home");
    }
}
